/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paquetevistas.compra;

import Seguridad.Controlador.Validacion;
import controlador.compra.controladorcompra;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import modelos.Compra;

/**
 *
 * @author Jacklin
 */
public class ModificarCompra extends javax.swing.JInternalFrame {

    /**
     * Creates new form ModificarCompra
     */
    public static ReNuevaCompra3 reasignarCompra;
    public static controladorcompra mycontroladorcompra = new controladorcompra();
    public static Validacion validacion = new Validacion();

    public ModificarCompra() {
        initComponents();
        loadComponents();
    }

    void loadComponents() {
        lbj.LBJ.Login.escritorio.compra.refreshPurchaseTable();
        Compra compra = lbj.LBJ.Login.escritorio.compra.compraaeditar;

        jprecioAcordado.setText("" + compra.getPrecioacordado());
        jComboEstado.setSelectedItem(compra.getEstado());
        jDateFechaReal.setDate(compra.getFechaacordada());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jprecioAcordado = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jComboEstado = new javax.swing.JComboBox();
        jDateFechaReal = new com.toedter.calendar.JDateChooser();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jButton1 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Editar Compra");

        jLabel2.setText("Fecha Real:");
        jLayeredPane1.add(jLabel2);
        jLabel2.setBounds(20, 30, 120, 10);

        jLabel3.setText("Precio Acordado:");
        jLayeredPane1.add(jLabel3);
        jLabel3.setBounds(20, 60, 130, 14);
        jLayeredPane1.add(jprecioAcordado);
        jprecioAcordado.setBounds(200, 60, 150, 20);

        jLabel1.setText("Estado:");
        jLayeredPane1.add(jLabel1);
        jLabel1.setBounds(20, 90, 120, 30);

        jComboEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "En Proceso", "Anulada" }));
        jLayeredPane1.add(jComboEstado);
        jComboEstado.setBounds(200, 100, 150, 20);
        jLayeredPane1.add(jDateFechaReal);
        jDateFechaReal.setBounds(200, 20, 150, 20);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/save.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jLayeredPane2.add(jButton1);
        jButton1.setBounds(60, 10, 65, 41);

        jLabel9.setText("Guardar");
        jLayeredPane2.add(jLabel9);
        jLabel9.setBounds(70, 60, 70, 14);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/limpiar.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jLayeredPane2.add(jButton2);
        jButton2.setBounds(160, 10, 59, 40);

        jLabel11.setText("Limpiar");
        jLayeredPane2.add(jLabel11);
        jLabel11.setBounds(170, 60, 70, 14);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/error 1.png"))); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jLayeredPane2.add(jButton5);
        jButton5.setBounds(250, 10, 60, 40);

        jLabel12.setText("Cancelar");
        jLayeredPane2.add(jLabel12);
        jLabel12.setBounds(260, 60, 60, 14);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        jDateFechaReal.setDate(null);
        jprecioAcordado.setText("");
        jComboEstado.setSelectedIndex(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        if (jDateFechaReal.getDate() == null) {
            JOptionPane.showMessageDialog(null, "Debe colocar una fecha");
        } else if (validacion.es_vacio(jprecioAcordado.getText())) {
            JOptionPane.showMessageDialog(this, validacion.msj + "Precio Acordado");
        } else if (!validacion.es_numero_real(jprecioAcordado.getText())) {
            JOptionPane.showMessageDialog(this, validacion.msj);
        } else {
            Float numcantidad = Float.parseFloat(jprecioAcordado.getText());
            if (jprecioAcordado.getText().equalsIgnoreCase("0") || (numcantidad < 0)) {
                JOptionPane.showMessageDialog(this, "Debe agregar una cantidad mayor a 0 de precio");
            } else {
                if (JOptionPane.showConfirmDialog(rootPane, "¿Desea realmente editar esta compra?",
                        "Mensaje", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    if (jComboEstado.getSelectedIndex() == 1) {
                        reasignarCompra = new ReNuevaCompra3(this, lbj.LBJ.Login.escritorio.compra.compraaeditar);
                        getDesktopPane().add(reasignarCompra);
                        reasignarCompra.setVisible(true);
                    } else {
                        Compra compra = lbj.LBJ.Login.escritorio.compra.compraaeditar;
                        Float precioAcordado = Float.parseFloat(jprecioAcordado.getText());
                        Date fechaAcordadaReal = jDateFechaReal.getDate();
                        String estado = jComboEstado.getSelectedItem().toString();
                        mycontroladorcompra.modificarcompra(compra, precioAcordado, fechaAcordadaReal, estado);
                        lbj.LBJ.Login.escritorio.compra.refreshPurchaseTable();
                        //this.setVisible(false);
                        JOptionPane.showMessageDialog(this, "La compra se editó correctamente");
                        this.dispose();
                    }
                }
            }

        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        if(JOptionPane.showConfirmDialog(null, "¿Está seguro de cancelar la edición?",
            "Mensaje",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
        lbj.LBJ.Login.escritorio.compra.refreshPurchaseTable();
        this.dispose();
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboEstado;
    private com.toedter.calendar.JDateChooser jDateFechaReal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JTextField jprecioAcordado;
    // End of variables declaration//GEN-END:variables
}
